﻿using System;
using System.Windows;

namespace TFS.Kanban.Commons.Behaviors
{
    public static class TreeView
    {
        #region CanTrackSelectedItem

        public static readonly DependencyProperty CanTrackSelectedItemProperty =
            DependencyProperty.RegisterAttached("CanTrackSelectedItem", typeof(bool), typeof(TreeView),
                                                new PropertyMetadata(CanTrackSelectedItemChanged));

        public static object GetCanTrackSelectedItem(DependencyObject dep)
        {
            return dep.GetValue(SelectedItemProperty);
        }

        public static void SetCanTrackSelectedItem(DependencyObject dep, object value)
        {
            dep.SetValue(SelectedItemProperty, value);
        }

        private static void CanTrackSelectedItemChanged(DependencyObject dep, DependencyPropertyChangedEventArgs e)
        {
            if (dep is System.Windows.Controls.TreeView)
            {
                var treeView = (System.Windows.Controls.TreeView) dep;
                var value = (bool) e.NewValue;

                if (value)
                {
                    treeView.SelectedItemChanged += SelectedItemChanged;
                }
                else
                {
                    treeView.SelectedItemChanged -= SelectedItemChanged;
                }
            }
        }


        private static void SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var tree = (System.Windows.Controls.TreeView)sender;
            SetSelectedItem(tree, e.NewValue);
            SetSelectedValue(tree, tree.SelectedValue);
        }

        #endregion

        #region SelectedItem

        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(TreeView), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public static object GetSelectedItem(DependencyObject dep)
        {
            return dep.GetValue(SelectedItemProperty);
        }

        public static void SetSelectedItem(DependencyObject dep, object value)
        {
            dep.SetValue(SelectedItemProperty, value);
        }

        #endregion

        #region SelectedValue

        public static readonly DependencyProperty SelectedValueProperty =
            DependencyProperty.RegisterAttached("SelectedValue", typeof(object), typeof(TreeView), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public static object GetSelectedValue(DependencyObject dep)
        {
            return dep.GetValue(SelectedValueProperty);
        }

        public static void SetSelectedValue(DependencyObject dep, object value)
        {
            dep.SetValue(SelectedValueProperty, value);
        }

        #endregion
    }
}
